package com.siblings.loan.ui.loan.act

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.Typeface
import android.os.Handler
import android.os.Looper
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.View
import com.lib.base.base.BaseActivity
import com.lib.base.utils.thread.TimingThread
import com.siblings.loan.R
import com.siblings.loan.databinding.ActLoanApplyWaitBinding
import com.siblings.loan.http.HttpHelper
import com.siblings.loan.utils.dialog.KefuDialog
import com.siblings.loan.utils.event.SibEventType

/**
 * SiblingsLoan
 * @Author wwdeng
 * @CreateTime: 2024/7/12 18:45
 * @Annotation：申请贷款等待
 */
class SibLoanApplyWaitAct: BaseActivity(),TimingThread.ITimingThreadListener {
    lateinit var binding: ActLoanApplyWaitBinding
    private var timeThread: TimingThread? = null
    //等待时长
    private var countdown = 9

    companion object {
        private const val INTENT_KEY_AMOUNT = "amount"
        private const val INTENT_KEY_DAYS = "days"
        fun start(context: Context, amount: String,days: String) {
            val intent = Intent(context, SibLoanApplyWaitAct::class.java)
            intent.putExtra(INTENT_KEY_AMOUNT, amount)
            intent.putExtra(INTENT_KEY_DAYS, days)
            context.startActivity(intent)
        }
    }

    override fun getSibRootView(): View {
        binding = ActLoanApplyWaitBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initSibView() {
        setSibTitleBarColor(Color.parseColor("#FFFFFFFF"))
        setSibTextTitle("")
        setSibBackBtnVisibility(View.INVISIBLE)
        setSibRightBtnRes(R.mipmap.pic_kefu_theme)
    }

    override fun initSibListener() {
        //联系客服
        rootViewBinding.ivFun.setOnClickListener {
            showSibKefuDialog("Loan Apply Wait Page")
            HttpHelper.addSibEventAction(SibEventType.CLICK.SIB_CLICK_CUSTOMER, "Loan Apply Wait Page")
        }
    }

    override fun initSibData() {
        startTimeThread()
    }

    /**
     * 启动倒计时
     */
    private fun startTimeThread() {
        if (timeThread != null) {
            timeThread!!.stopThread()
        }
        //随机 10秒 等待时长
        countdown = 9
        showSibTipsText()
        if (timeThread == null) {
            timeThread = TimingThread()
            timeThread!!.setThreadDelayMillis(1000)
            timeThread!!.setLoadListener(this)
        }
        timeThread!!.startThread()
    }

    override fun onLoading() {
        if (countdown <= 1){
            countdown = 9
            timeThread!!.stopThread()
            showSibLoading(isTOuchOut = false, isCancel = true)
            showSibLoanApplySuccess()
            return
        }
        countdown--
        showSibTipsText()
    }

    /**
     * 设置文本
     */
    private fun showSibTipsText(){
        val textSMS = getString(R.string.loan_apply_wait_sms)
        val showText = String.format(getString(R.string.loan_apply_wait_tips),textSMS,"$countdown")

        val smsIndex = showText.indexOf(textSMS)
        val spString = SpannableString(showText)
        spString.setSpan(ForegroundColorSpan(Color.parseColor("#FFFF0000")), smsIndex, smsIndex + textSMS.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        spString.setSpan(StyleSpan(Typeface.BOLD), smsIndex, smsIndex + textSMS.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
        binding.textWaitTips.text = spString

        showSibStepStatusView()
    }

    /**
     * 显示客服dialog
     */
    private fun showSibKefuDialog(eventRemark: String){
        val kefuDialog = KefuDialog(this,eventRemark,object : KefuDialog.IDialogCallback{
            override fun onClick(view: View?) {
            }
        })
        kefuDialog.setCancelable(true)
        kefuDialog.setCanceledOnTouchOutside(true)
        kefuDialog.show()
    }

    /**
     * 贷款订单创建成功
     */
    private fun showSibLoanApplySuccess(){
        Handler(Looper.getMainLooper()).postDelayed({
            hideSibLoading()
            //返回首页
            finish()
        },500)
    }

    /**
     * 获取贷款金额
     */
    private fun getSibApplyAmount(): String? {
        return intent.extras!!.getString(INTENT_KEY_AMOUNT)
    }

    /**
     * 获取贷款时长
     */
    private fun getSibApplyDays(): String? {
        return intent.extras!!.getString(INTENT_KEY_DAYS)
    }

    /**
     * 显示状态
     */
    private fun showSibStepStatusView(){
        if (countdown in 7..9){
            binding.imgStep1.setImageResource(R.mipmap.pic_step_check_wait_step2)
            binding.vStep1Bottom.setBackgroundColor(Color.parseColor("#FFBBBBBB"))

            binding.vStep2Top.setBackgroundColor(Color.parseColor("#FFBBBBBB"))
            binding.imgStep2.setImageResource(R.mipmap.pic_step_check_wait_step3)
            binding.vStep2Bottom.setBackgroundColor(Color.parseColor("#FFBBBBBB"))

            binding.vStep3Top.setBackgroundColor(Color.parseColor("#FFBBBBBB"))
            binding.imgStep3.setImageResource(R.mipmap.pic_step_check_wait_step3)
            return
        }

        if (countdown in 4..6){
            binding.imgStep1.setImageResource(R.mipmap.pic_step_check_wait_step1)
            binding.vStep1Bottom.setBackgroundColor(Color.parseColor("#FF4DB673"))

            binding.vStep2Top.setBackgroundColor(Color.parseColor("#FF4DB673"))
            binding.imgStep2.setImageResource(R.mipmap.pic_step_check_wait_step2)
            binding.vStep2Bottom.setBackgroundColor(Color.parseColor("#FFBBBBBB"))

            binding.vStep3Top.setBackgroundColor(Color.parseColor("#FFBBBBBB"))
            binding.imgStep3.setImageResource(R.mipmap.pic_step_check_wait_step3)
            return
        }
        binding.imgStep1.setImageResource(R.mipmap.pic_step_check_wait_step1)
        binding.vStep1Bottom.setBackgroundColor(Color.parseColor("#FF4DB673"))

        binding.vStep2Top.setBackgroundColor(Color.parseColor("#FF4DB673"))
        binding.imgStep2.setImageResource(R.mipmap.pic_step_check_wait_step1)
        binding.vStep2Bottom.setBackgroundColor(Color.parseColor("#FF4DB673"))

        binding.vStep3Top.setBackgroundColor(Color.parseColor("#FF4DB673"))
        binding.imgStep3.setImageResource(R.mipmap.pic_step_check_wait_step2)
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addSibEventAction(SibEventType.PAGE.SIB_PAGE_LOAN_APPLY_WAIT_START, "")
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addSibEventAction(SibEventType.PAGE.SIB_PAGE_LOAN_APPLY_WAIT_END, "")
        if (timeThread != null) {
            timeThread!!.stopThread()
            timeThread = null
        }
    }
}